home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / dev / misc / Gfx4PCQ.lha / WindowLib / Examples / SpriteDemo / SpriteDemo.p < prev    next >
Encoding:
Text File  |  1997-07-19  |  7.1 KB  |  232 lines

  1. Program SpriteDemo;
  2.  
  3. {"Thousands sprites" demo for the windowib. © 1997 THOR-Software.
  4.  
  5.   This example program displays quite a lot of hardware sprites and
  6.   presents a simple use of the animation engine.}
  7.  
  8. Procedure WaitTOF;
  9. External;
  10. {$I "include:utils/windowlib.i"}
  11. {$I "include:utils/stringlib.i"}
  12. {$I "include:utils/random.i"}
  13.   
  14. PROCEDURE Demo;
  15.  
  16. TYPE    
  17.     SpriteArray     =     Array[0..8] of String;        {A single sprite shape}
  18.     Anim        =    Array[1..13] of SpritePtr;    {This holds the complete animation as one sprite}
  19.  
  20. CONST
  21.     {This is the definition of the sprite shapes as ASCII-ART
  22.      See the windowlib.guide to find out more about the encoding here.}
  23.  
  24.         Shapes : Array[1..13] of SpriteArray=(
  25.                 ("     ...     ",
  26.                  "   ..++...   ",
  27.                  "  .+.+.+.+.  ",
  28.                  "  ++.++..+.  ",
  29.                  "   +.+....   ",
  30.                  "     ...     ",
  31.                  "    * * *    ",
  32.                  "    *****    ",
  33.                  "    *****    "),
  34.  
  35.                 ("     ...     ",
  36.                  "   .++...+   ",
  37.                  "  ..+.+.+..  ",
  38.                  "  ..++..+..  ",
  39.                  "   .+....+   ",
  40.                  "     ...     ",
  41.                  "    * * *    ",
  42.                  "    *****    ",
  43.                  "    *****    "),
  44.  
  45.                 ("     ...     ",
  46.                  "   ++...++   ",
  47.                  "  .+.+.+...  ",
  48.                  "  .++..+...  ",
  49.                  "   +....++   ",
  50.                  "     ...     ",
  51.                  "    * * *    ",
  52.                  "    *****    ",
  53.                  "    *****    "),
  54.  
  55.                 ("     ...     ",
  56.                  "   +...++.   ",
  57.                  "  +.+.+....  ",
  58.                  "  ++..+....  ",
  59.                  "   ....++.   ",
  60.                  "     ...     ",
  61.                  "    * * *    ",
  62.                  "    *****    ",
  63.                  "    *****    "),
  64.  
  65.                 ("     ...     ",
  66.                  "   ...++..   ",
  67.                  "  .+.+...+.  ",
  68.                  "  +..+...+.  ",
  69.                  "   ...++..   ",
  70.                  "     ...     ",
  71.                  "    * * *    ",
  72.                  "    *****    ",
  73.                  "    *****    "),
  74.  
  75.                 ("     ...     ",
  76.                  "   ..++..+   ",
  77.                  "  +.+...+..  ",
  78.                  "  ..+...+..  ",
  79.                  "   ..++..+   ",
  80.                  "     ...     ",
  81.                  "    * * *    ",
  82.                  "    *****    ",
  83.                  "    *****    "),
  84.  
  85.                 ("     ...     ",
  86.                  "   .++..++   ",
  87.                  "  .+...+..+  ",
  88.                  "  .+...+.++  ",
  89.                  "   .++..++   ",
  90.                  "     ...     ",
  91.                  "    * * *    ",
  92.                  "    *****    ",
  93.                  "    *****    "),
  94.  
  95.                 ("     ...     ",
  96.                  "   ++..++.   ",
  97.                  "  +...+..+.  ",
  98.                  "  +...+.++.  ",
  99.                  "   ++..+++   ",
  100.                  "     ...     ",
  101.                  "    * * *    ",
  102.                  "    *****    ",
  103.                  "    *****    "),
  104.  
  105.                 ("     ...     ",
  106.                  "   +..++..   ",
  107.                  "  ...+..+.+  ",
  108.                  "  ...+.++.+  ",
  109.                  "   +..+++.   ",
  110.                  "     ...     ",
  111.                  "    * * *    ",
  112.                  "    *****    ",
  113.                  "    *****    "),
  114.  
  115.                 ("     ...     ",
  116.                  "   ..++..+   ",
  117.                  "  ..+..+.+.  ",
  118.                  "  ..+.++.+.  ",
  119.                  "   ..+++.+   ",
  120.                  "     ...     ",
  121.                  "    * * *    ",
  122.                  "    *****    ",
  123.                  "    *****    "),
  124.  
  125.                 ("     ...     ",
  126.                  "   .++..++   ",
  127.                  "  .+..+.+.+  ",
  128.                  "  .+.++.++.  ",
  129.                  "   .+++.+.   ",
  130.                  "     ...     ",
  131.                  "    * * *    ",
  132.                  "    *****    ",
  133.                  "    *****    "),
  134.  
  135.                 ("     ...     ",
  136.                  "   ++..++.   ",
  137.                  "  +..+.+.+.  ",
  138.                  "  +.++.++..  ",
  139.                  "   +++.+..   ",
  140.                  "     ...     ",
  141.                  "    * * *    ",
  142.                  "    *****    ",
  143.                  "    *****    "),
  144.  
  145.                 ("     ...     ",
  146.                  "   +..++..   ",
  147.                  "  ..+.+.+.+  ",
  148.                  "  .++.++..+  ",
  149.                  "   ++.+...   ",
  150.                  "     ...     ",
  151.                  "    * * *    ",
  152.                  "    *****    ",
  153.                  "    *****    "));
  154.  
  155. NumSprites    =    16;    {number of sprites to display. Increase for more!}
  156.                 
  157. VAR
  158.         s       :       ScreenPtr;
  159.         w       :       WindowPtr;    {screen and window}
  160.     objects    :    Array[1..NumSprites] of Anim;
  161.         sp      :       Array[1..NumSprites] of SpritePtr;
  162.         i,j     :       Integer;
  163.     x,y    :    Array[1..NumSprites] of Integer;
  164.     xv,yv    :    Array[1..NumSprites] of Integer;
  165.     xm,ym    :    Short;
  166.     r,g,b    :    Array[1..3] of Short;    {colors}
  167.             
  168. BEGIN
  169.     {open screen and window}
  170.         s:=OpenAScreen(0,0,640,256,2,MON_HIRES,"Sprite example");
  171.         IF s<>NIL THEN BEGIN
  172.                 
  173.                 w:=OpenScreenWindow(s,0,0,640,256,2+4+8,"Quite a lot of sprites");
  174.                 
  175.                 IF w<>NIL THEN BEGIN
  176.  
  177.             FOR j:=1 TO NumSprites DO BEGIN
  178.                 {roll dice to get the color}
  179.                 FOR i:=1 to 3 DO BEGIN
  180.                     r[i]:=RangeRandom(16);
  181.                     g[i]:=RangeRandom(16);
  182.                     b[i]:=RangeRandom(16);
  183.                 END;
  184.                 {now build the sprites, one for each animation component}
  185.                 FOR i:=1 TO 13 DO BEGIN
  186.                     objects[j][i]:=OpenSprite(w,@Shapes[i][0],9,SPRITE_HARDWARE); {we use real sprites here}
  187.                     SetSpriteColor(objects[j][i],1,r[1],g[1],b[1]);
  188.                     SetSpriteColor(objects[j][i],2,r[2],g[2],b[2]);
  189.                     SetSpriteColor(objects[j][i],3,r[3],g[3],b[3]);    {define colors}
  190.                     IF i>1 THEN
  191.                         LinkSprite(objects[j][1],objects[j][i]);{link it to the base sprite to get an animation}
  192.                 END;    
  193.                 sp[j]:=objects[j][1];    {we need the base only}
  194.             END;
  195.  
  196.             {get positions and velocities}    
  197.             FOR i:=1 TO NumSprites DO BEGIN
  198.                 x[i]:=RangeRandom(639) SHL 16;
  199.                 y[i]:=RangeRandom(229) SHL 16;
  200.                 xv[i]:=RangeRandom(2 SHL 8) SHL 8;
  201.                 yv[i]:=RangeRandom(2 SHL 8) SHL 8;
  202.             END;
  203.  
  204.                         RequestStart(w,CLOSEWINDOW_f);
  205.                         REPEAT
  206.                 FOR i:=1 To NumSprites DO BEGIN
  207.                     PlaceSprite(sp[i],x[i],y[i]);    {set sprite position}
  208.                     x[i]:=x[i]+xv[i];        {update position by velocity}
  209.                     IF (x[i]<=0) or (x[i]>=(639 SHL 16)) THEN
  210.                         xv[i]:=-xv[i];
  211.                     y[i]:=y[i]+yv[i];
  212.                     IF (y[i]<=0) or (y[i]>=(229 SHL 16)) THEN
  213.                         yv[i]:=-yv[i];
  214.                 END;
  215.                                 AnimateSprites(w);    {display sprites on the screen}
  216.                         UNTIL (NextRequest(w) AND CLOSEWINDOW_f)<>0;
  217.             {until done!}            
  218.         
  219.             {we don't need to close the sprites manually, this is done by the windowlib}
  220.                         CloseAWindow(w);
  221.                 END;
  222.                 CloseAScreen(s);
  223.         END;
  224. END;
  225.  
  226. BEGIN
  227.         InitGraphics;
  228.     SelfSeed;
  229.         Demo;
  230.     ExitGraphics;
  231. END.
  232.